home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / cexpress / strings / padcentr.asm < prev    next >
Encoding:
Assembly Source File  |  1989-05-04  |  1.9 KB  |  75 lines

  1. ;void  pad_center(strg,ch,position,length);
  2. ;  unsigned char  *strg,ch;
  3. ;  unsigned short  position,length;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _pad_center
  11. _pad_center proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;save DS
  17.     mov  _error_code,1    ;1 = error
  18.     cmp  _memory_model,0    ;near or far?
  19.     jle  begin        ;jump if near
  20.     inc  bp            ;else add 2 to BP
  21.     inc  bp            ;
  22. begin:    cmp  _memory_model,2    ;data near or far?
  23.     jb   L0            ;jump if near
  24.     les  di,dword ptr[bp+4] ;get string address
  25.     mov  ax,es        ;DS = ES
  26.     mov  ds,ax        ;
  27.     inc  bp            ;add 2 to bp since dword ptr
  28.     inc  bp            ;
  29.     jmp  short L1        ;jump ahead
  30. L0:    mov  di,[bp+4]        ;near case
  31.     mov  ax,ds        ;
  32.     mov  es,ax        ;
  33. L1:    sub  cx,cx        ;must find string length
  34.     mov  si,di        ;copy string ptr
  35. L2:    cmp  byte ptr[si],0     ;end of string?
  36.     je   L3            ;
  37.     inc  si            ;
  38.     inc  cx            ;
  39.     jmp  short L2        ;loop till null
  40. L3:    sub  bx,bx        ;
  41.     mov  bl,[bp+10]        ;new string length in BL
  42.     cmp  cl,bl        ;compare the two lengths
  43.     jae  L5            ;quit if old len >= new
  44.     add  di,bx        ;pt DI to new string end
  45.     sub  bx,cx        ;number of spaces to pad
  46.     mov  ax,[bp+8]        ;starting position
  47.     inc  ax            ;count from 1
  48.     inc  cx            ;add one to counter for null
  49.     cmp  ax,cx        ;within string?
  50.     ja   L5            ;quit if outside string
  51.     sub  cx,ax        ;
  52.     inc  cx            ;adjust counter
  53.     std            ;set direction flag
  54.     rep  movsb        ;move old string right
  55.     mov  cx,bx        ;spaces to pad in CX
  56.     mov  al,[bp+6]        ;get pad character
  57. L4:    mov  es:[di],al        ;insert pad char
  58.     dec  di            ;move to next position
  59.     loop L4            ;go do next char
  60.     pop  ds            ;restore DS
  61.     dec  _error_code    ;0 = no error
  62.     jmp  short L6        ;jump ahead and quit
  63. L5:    pop  ds            ;exit with error
  64. L6:    pop  si            ;
  65.     pop  di            ;
  66.     pop  bp            ;
  67.     cld            ;
  68.     cmp  _memory_model,0    ;quit
  69.     jle  quit        ;
  70.     db   0CBh        ;RET far
  71. quit:    ret            ;RET near
  72. _pad_center ENDP
  73. _TEXT    ENDS
  74.     END
  75.